package online.sixfish.doubao.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import online.sixfish.common.api.ApiErrorCode;
import online.sixfish.common.dto.PageDTO;

import online.sixfish.common.eunmration.ContentAuditConclusionTypeEnum;
import online.sixfish.common.exception.ApiAsserts;
import online.sixfish.common.exception.CommonConstant;
import online.sixfish.common.utils.ContentAuditUtil;
import online.sixfish.common.utils.SecurityUtil;
import online.sixfish.doubao.mapper.ArticleMapper;
import online.sixfish.doubao.model.dto.AddArticleDTO;
import online.sixfish.doubao.model.entity.Article;
import online.sixfish.doubao.model.entity.Tag;
import online.sixfish.doubao.service.ArticleService;
import online.sixfish.doubao.service.IBmsTagService;
import online.sixfish.doubao.service.IBmsTopicTagService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Service
public class ArticelserviceImpl implements ArticleService {
    @Resource
    ContentAuditUtil contentAuditUtil;
    @Resource
    IBmsTagService iBmsTagService;
    @Resource
    IBmsTopicTagService iBmsTopicTagService;
    @Resource
    ArticleMapper articleMapper;

    @Override
    public PageDTO<Article> list(Integer type, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getType, type);
        queryWrapper.orderByDesc(Article::getTop);
        queryWrapper.ne(Article::getStatus, CommonConstant.SYS_ILLEGAL_STATUS);
        Page<Article> page = new Page<>(pageNum, pageSize);
        articleMapper.selectPage(page, queryWrapper);
        return PageDTO.build(page);

    }

    @Override
    public String add(AddArticleDTO addArticleDTO) {
        try {
            ContentAuditConclusionTypeEnum result = contentAuditUtil
                    .audit(addArticleDTO.getTitle() + addArticleDTO.getContent() + addArticleDTO.getTags());
            if (!Objects.equals(result.getId(), ContentAuditConclusionTypeEnum.COMPLIANT.getId())) {
                ApiAsserts.fail(ApiErrorCode.CONTENT_AUDIT_NOT_PASS);
            }

        } catch (IOException e) {
            //接口调用失败
            ApiAsserts.fail(ApiErrorCode.CONTENT_AUDIT_ERROR);
        }
        Article article = new Article();
        BeanUtils.copyProperties(addArticleDTO, article);
        article.setUserId(SecurityUtil.currentUserId());
        articleMapper.insert(article);
        //tag
        if (CollectionUtils.isNotEmpty(addArticleDTO.getTags())) {
            List<Tag> tags = iBmsTagService.insertTags(addArticleDTO.getTags());
            iBmsTopicTagService.createTopicTag(article.getId(), tags);
        }

        return article.getId();


    }

    @Override
    public Article getById(String id) {
        return articleMapper.selectById(id);
    }

    @Override
    public void viewIncrement(String articleId) {
        LambdaUpdateWrapper<Article> wrapper = Wrappers
                .lambdaUpdate(Article.class).eq(Article::getId, articleId);
        wrapper.setSql("view=view+1");
        articleMapper.update(null, wrapper);
    }

    @Override
    public List<Article> getRecommend(String id) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(Article::getId, id);
        wrapper.orderByDesc(Article::getView);
        wrapper.last("limit 10");
        List<Article> list = articleMapper.selectList(wrapper);
        return list;

    }

    @Override
    public int getUserArticleCount(String id) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(Article::getUserId, id);
        return articleMapper.selectCount(wrapper);
    }

    @Override
    public PageDTO select(Integer type, Integer sortType, String title, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(type)) {
            queryWrapper.eq(Article::getType, type);

        }
        if (Objects.nonNull(sortType)) {
            if (sortType.equals(1)) {
                //浏览量降序
                queryWrapper.orderByDesc(Article::getView);
            }
        }
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like(Article::getTitle, title);
        }
        queryWrapper.ne(Article::getStatus, CommonConstant.SYS_ILLEGAL_STATUS);
        Page<Article> page = new Page<>(pageNum, pageSize);
        articleMapper.selectPage(page, queryWrapper);
        return PageDTO.build(page);
    }


    @Override
    public void deleteArticle(String id) {
        Article article = new Article();
        article.setId(id);
        article.setStatus(CommonConstant.SYS_ILLEGAL_STATUS);
        articleMapper.updateById(article);
    }

    @Override
    public void updateArticleTop(String id, Integer isTop) {
        Article article = new Article();
        article.setId(id);
        if (isTop.equals(1)) {
            article.setTop(Boolean.TRUE);
        } else {
            article.setTop(Boolean.FALSE);
        }
        articleMapper.updateById(article);
    }

    @Override
    public PageDTO getArticleByUserId(String userId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getUserId, userId);
        wrapper.ne(Article::getStatus, CommonConstant.SYS_ILLEGAL_STATUS);
        Page<Article> page = new Page<>(pageNum, pageSize);
        articleMapper.selectPage(page, wrapper);
        return PageDTO.build(page);
    }

    @Override
    public PageDTO search(String keyword, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Article::getTitle,keyword);
        wrapper.like(Article::getContent,keyword);
        wrapper.ne(Article::getStatus, CommonConstant.SYS_ILLEGAL_STATUS);
        Page<Article> page = new Page<>(pageNum, pageSize);
        articleMapper.selectPage(page, wrapper);
        return PageDTO.build(page);
    }


}
